package ServletMerchant;

import Dao.MctDao;
import Entity.Merchant;
import Util.Page;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.sql.SQLException;
import java.util.List;

@WebServlet(name = "ServletMctUpdate", value = "/mctupdate")
public class ServletMctUpdate extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        int id = Integer.parseInt(request.getParameter("id"));

        Merchant merchant = null;
        try {
            merchant = MctDao.queryByid(id);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }

        List<Merchant> list = null;
        try {
            list = MctDao.queryByKey("", merchant.getUsername());
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        //获取页码
        int pagenum = 1,total = list.size();
        pagenum = Integer.parseInt(request.getParameter("pagenum"));
        Page page = new Page(pagenum,total);
        request.setAttribute("page",page);
//        System.out.println("get:"+page);

        request.setAttribute("merchant", merchant);
        request.setAttribute("list",list);
        request.setAttribute("username", merchant.getUsername());
//        System.out.println(merchant.getUsername());
        request.getRequestDispatcher("mctupdate.jsp").forward(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");

        int id = Integer.parseInt(request.getParameter("id"));
        String name = request.getParameter("name");

        String belongto = request.getParameter("belongto");
        String username = request.getParameter("username");
        Merchant merchant = new Merchant(id, name, belongto, username);
//        System.out.println(merchant);
        try {
            MctDao.update(merchant);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        request.setAttribute("merchant", merchant);

        List<Merchant> list = null;
        try {
            list = MctDao.queryByKey("",username);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }

        //获取页码
        int pagenum = 1,total = list.size();
        pagenum = Integer.parseInt(request.getParameter("pagenum"));
        Page page = new Page(pagenum,total);
        request.setAttribute("page",page);
//        System.out.println("get:"+page);

        request.setAttribute("list", list);
        request.setAttribute("username",username);
//        System.out.println(username);
        request.getRequestDispatcher("mctupdate.jsp").forward(request, response);
    }
}
